package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AccountRechargeService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.AccountRechargeModel;

/**
 * [后台]充值管理
 * @author linya
 */
public class ManageAccountRechargeAction extends BaseAction<AccountRechargeModel> implements ModelDriven<AccountRechargeModel> {

	@Resource
	private AccountRechargeService accountRechargeService;

	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * [后台] 充值管理页面
	 * @author linya
	 */
	@Action("/modules/account/accountRecharge/accountRechargeManager")
	public String accountRechargeManager() throws Exception {
		return "accountRechargeManager";
	}

	/**
	 * 充值记录
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/accountRecharge/accountRechargeList")
	public void accountRechargeList() throws Exception {
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		if(StringUtil.isBlank(paramString("type"))){
			model.setType(99);
		}
		
		PageDataList<AccountRechargeModel> pageList = accountRechargeService.list(model);
		int totalPage = pageList.getPage().getTotal();
		map.put("total", totalPage);
		map.put("rows", pageList.getList());
		printJson(getStringOfJpaObj(map));
	}

	/**
	 * 导出报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/accountRecharge/exportExcelRecharge")
	public void exportExcelRecharge() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(99);
		}
		if(StringUtil.isBlank(paramString("type"))){
			model.setType(99);
		}
		// 第一页开始
		model.setPage(1);
		// 最多出5000条记录
		model.setRows(5000);
		model.setSort("id");
		model.setOrder("desc");
		List<AccountRechargeModel> list = accountRechargeService.list(model).getList();
		String title = "充值管理Excel表_"+DateUtil.dateStr3(DateUtil.getNow());
		String[] hearders = new String[] { "编号", "流水号", "用户名", "真实姓名","充值金额(元)", "到账金额(元)", "手续费(元)","手续费承担方","充值类型", "支付方式", "充值时间", "状态" };// 表头数组
		String[] fields = new String[] { "id", "tradeNo", "userName", "realName", "money", "amountIn", "fee", "rechargeFeeBearName","typeStr","payment", "addTime", "statusStr"};// 对象属性数组
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}	
	
}
